<?php
namespace Common\Model;

use Think\Model;

class PayModel extends Model
{

    protected function _initialize()
    {
        $this->trueTableName = 'lockpays';
    }

    /**
     * 保存订单
     *
     */
    public function savePays($pays)
    {
        $id = $this->add($pays);
        return $id;
    }

    /**
     * 根据订单ID获取订单信息 生成订单的时候 使用lockorder表 id 作为订单id
     */
    public function getPayInfoByPayID($payID)
    {
        $sql =  "SELECT * FROM lockorder WHERE id='{$payID}'";

        $query = $this->query($sql);

        return $query;

    }

    public function getPaysByOrderID($orderID)
    {
        $sql = "SELECT * FROM lockpays WHERE order_id='{$orderID}'";
        $query = $this->query($sql);

        return $query;
    }

    /**
     * 查询微客栈订单详细
     */
    public function getWkzBillByDate($data){
        $return = array();
        $innid = $data['innid'];
        $fromdate = $data['fromdate'];
        $enddate = $data['enddate'];
        $service = $data['service'];
        $paytype = $data['paytype'];
        $orderfrom = $data['orderfrom'];

        // TODO check summary
        $sql = "SELECT * FROM wx_pays p
                LEFT JOIN wx_orders o ON p.order_id=o.id
                WHERE o.innid='{$innid}'
                AND o.statussummary='placed'
                AND o.service='{$service}'
                AND p.pay_type='gateway'
                AND DATE_FORMAT(o.addtime,'%Y-%m-%d') BETWEEN '{$fromdate}' AND '{$enddate}'";
        $where = "";
        if($paytype){
            $where.= " AND p.gateway='{$paytype}'";
        }
        if($orderfrom){
            $where.= " AND o.channelcode='{$orderfrom}'";
        }
        $sql = $sql . $where . " order by addtime desc";
        $query = $this->query($sql);
        foreach($query as $k=>$order){
            $tmp['roomname'] = $order['roomname'];
            $tmp['guestname'] = $order['guestname'];
            $tmp['guestphone'] = $order['guestphone'];
            $tmp['guestinfo'] = $order['guestname'] . "，" . $order['guestphone'];
            $tmp['totalprice'] = $order['fee'];
            $tmp['addtime'] = $order['addtime'];
            $tmp['gateway'] = $order['gateway'];
            $tmp['arriveinfo'] = substr($order['arrivedate'], 5) . "入住，住" . $order['nights'] . "晚";
            $tmp['orderfrom'] = $order["channelcode"];
            $tmp['paystatus'] = $order["pay_status"];
            $tmp['orderid'] = $order["order_id"];
            $tmp['createon'] = $order['addtime'];

            // 客栈版本支持住多间
            if ($service === 'inn') {
                $tmp['arriveinfo'] .= "，住" . $order['num'] . "间";
            }

            // 如果是物品单, 就去掉单子描述
            if ('goods' === $order['orderkind']) {
                $tmp['arriveinfo'] = '';
            }

            $return[] = $tmp;
        }
        return $return;
    }

    /**
     * 根据订单ID获取订单信息
     */
    public function getPayInfoByOrderID($orderID)
    {
        $sql = "SELECT p.*,o.innname,o.innid,o.roomname,o.arrivedate,o.nights
                FROM wx_pays p LEFT JOIN wx_orders o
                ON p.order_id=o.id
                WHERE p.order_id='{$orderID}'
                AND p.pay_type='gateway'";

        $query = $this->query($sql);
        if (empty($query)) {
            return false;
        } else {
            return $query[0];
        }
    }

    /**
     * 根据客栈id和结算日期获取订单信息
     */
    public function getOrderByBilldate($data)
    {
        $innid = $data['innid'];
        $date = $data['billdate'];
        $service = $data['service'];

        // TODO check summary
        $sql = "SELECT * FROM wx_pays p
                LEFT JOIN wx_orders o ON p.order_id=o.id
                WHERE o.innid='{$innid}'
                AND DATE_FORMAT(o.addtime,'%Y-%m-%d')='{$date}'
                AND o.statussummary='placed'
                AND o.service='{$service}'
                AND p.pay_type='gateway'
                AND p.pay_status='1'";
        $query = $this->query($sql);
        if (empty($query)) {
            return false;
        } else {
            return $query;
        }
    }

}


// end